package com.geek.service.impl;

import com.geek.bean.data.CategoryData;
import com.geek.bean.data.ResponseCatalogIndexData;
import com.geek.mapper.MarketCategoryMapper;
import com.geek.model.MarketCategory;
import com.geek.model.MarketCategoryExample;
import com.geek.service.CategoryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wbw
 * @data 2024/06/17 14:58
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Override
    public List queryLevelOne() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andLevelEqualTo("L1");
        example.setOrderByClause("add_time desc");
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);

        List<Map<String, Object>> collect = marketCategories.stream().map(marketCategory -> {
            Map<String, Object> map = new HashMap<>();
            map.put("iconUrl", marketCategory.getIconUrl());
            map.put("id", marketCategory.getId());
            map.put("name", marketCategory.getName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public ResponseCatalogIndexData index() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andLevelEqualTo("L1");
        List<MarketCategory> categories = categoryMapper.selectByExample(example);
        MarketCategory marketCategory = categories.get(0);
        List<MarketCategory> marketCategories = this.queryLevelTwo(marketCategory.getId());
        ResponseCatalogIndexData data = ResponseCatalogIndexData.builder()
                .categoryList(categories)
                .currentCategory(marketCategory)
                .currentSubCategory(marketCategories)
                .build();
        return data;
    }

    @Override
    public List<MarketCategory> queryLevelTwo(Integer id) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andLevelEqualTo("L2")
                .andPidEqualTo(id);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public Map<String, Object> current(Integer id) {
        Map<String, Object> map = new HashMap<>();
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);
        List<MarketCategory> marketCategories = this.queryLevelTwo(id);
        map.put("currentCategory",marketCategory);
        map.put("currentSubCategory",marketCategories);
        return map;
    }

    @Override
    public CategoryData category(Integer id) {
        CategoryData categoryData = new CategoryData();
        //根据id查找到所点击的L1/L2 
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);
        //根据Pid判断是L1/L2
        if (marketCategory.getPid() == 0) {
            //如果为L1，则id为L1的Id → 根据L2的Pid = id 查询L2
            MarketCategoryExample example = new MarketCategoryExample();
            example.createCriteria().andDeletedEqualTo(false)
                    .andPidEqualTo(id);
            List<MarketCategory> brotherCategory = categoryMapper.selectByExample(example);
            categoryData.setBrotherCategory(brotherCategory);
            categoryData.setCurrentCategory(brotherCategory.get(0));
            categoryData.setParentCategory(marketCategory);
        }else {
            ////如果为L2，则id为L2的Id → 所选择的marketCategory → 根据Pid再次找出属于同一个L1的显示
            MarketCategoryExample example = new MarketCategoryExample();
            example.createCriteria().andDeletedEqualTo(false)
                    .andPidEqualTo(marketCategory.getPid());
            List<MarketCategory> brotherCategory = categoryMapper.selectByExample(example);
            MarketCategory parentCategory = categoryMapper.selectByPrimaryKey(marketCategory.getPid());
            categoryData.setBrotherCategory(brotherCategory);
            categoryData.setCurrentCategory(marketCategory);
            categoryData.setParentCategory(parentCategory);
        }
        return categoryData;
    }

    @Override
    public List<MarketCategory> list(String l1, int page, int categoryLimit) {
        MarketCategoryExample example = new MarketCategoryExample();
        PageHelper.startPage(page, categoryLimit);
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        return categoryMapper.selectByExample(example);
    }
}
